package server

import (
	"service-hello/api"
	"service-hello/internal/infrastructure/conf"
	"time"

	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/logging"
	"github.com/go-kratos/kratos/v2/middleware/recovery"
	"github.com/go-kratos/kratos/v2/transport/http"
)

// NewHttpServer new a http server.
func NewHttpServer(c *conf.HTTP, logger log.Logger, svc api.TemplateServer) *http.Server {
	var opts = []http.ServerOption{
		http.Address(c.Address),
		http.Timeout(30 * time.Second),
		http.Logger(logger),
		http.Middleware(
			recovery.Recovery(recovery.WithLogger(logger)),
			logging.Server(logger),
		),
	}
	srv := http.NewServer(opts...)

	srv.Route("/_invoke").POST("/{name}", invoke(svc))

	return srv
}
